package com.zyzd.custom.widget.attributedesc;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Space;

import com.zyzd.custom.util.IdUtils;

import java.util.ArrayList;

import static com.zyzd.custom.widget.attributedesc.BaseAttributeDescView.ItemType.LEFT;
import static com.zyzd.custom.widget.attributedesc.BaseAttributeDescView.ItemType.LINE;
import static com.zyzd.custom.widget.attributedesc.BaseAttributeDescView.ItemType.RIGHT;
import static com.zyzd.custom.widget.attributedesc.BaseAttributeDescView.BaseEntity;

/**
 * Created by lzy on 2018/6/4 0004.
 * description ${商品属性描述控件}
 * <p>
 * Changed by somebody on 2018/6/4 0004
 * description ${TODO}
 */
public abstract class BaseAttributeDescView<T extends BaseEntity> extends ConstraintLayout {

    private ArrayList<T> mData;
    private Space mAnchor;
    private LayoutParams mAnchorLp;


    public BaseAttributeDescView(Context context) {
        this(context, null);
    }

    public BaseAttributeDescView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseAttributeDescView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setData(ArrayList<T> lists) {
        mData = lists;
        handleData();
        this.removeAllViews();
        setAnchor();
        View preView = null;
        for (int i = 0; i < lists.size(); i++) {
            T item = lists.get(i);
            View view = createView(item);
            int id = IdUtils.generateViewId();
            view.setId(id);
            LayoutParams lp = null;
            lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
            if (i == 0) {
                lp.topToTop = getId();
                lp.leftToLeft = getId();
                lp.rightToRight = item.getType() == LEFT ? mAnchor.getId() : getId();
            } else {
                if (item.getType() == LEFT || item.getType() == LINE) {
                    lp.topToBottom = preView.getId();
                    lp.leftToLeft = getId();
                    lp.rightToRight = item.getType() == LEFT ? mAnchor.getId() : getId();
                } else {
                    lp.topToTop = preView.getId();
                    lp.bottomToBottom = preView.getId();
                    lp.leftToRight = preView.getId();
                    lp.rightToRight = getId();
                }
            }
            addView(view, lp);
            preView = view;
        }
    }

    private void setAnchor() {
        if (mAnchor == null) {
            mAnchor = new Space(getContext());
            mAnchor.setId(IdUtils.generateViewId());
        }
        if (mAnchorLp == null) {
            mAnchorLp = new LayoutParams(1, 1);
            mAnchorLp.leftToLeft = getId();
            mAnchorLp.rightToRight = getId();
            mAnchorLp.horizontalBias = 0.5f;
        }
        addView(mAnchor, mAnchorLp);
    }

    protected abstract View createView(T item);

    protected void handleData() {
        for (int i = 0; i < mData.size(); i++) {
            T item = mData.get(i);

            if (i == 0 || mData.get(i - 1).getType() == LINE || mData.get(i - 1).getType() == RIGHT) {
                item.setType(item.getContent().length() > getMaxLengh() ? LINE : LEFT);
            } else {
                item.setType(item.getContent().length() > getMaxLengh() ? LINE : RIGHT);
            }
        }
    }

    protected int getMaxLengh() {
        return 10;
    }

    public enum ItemType {
        LEFT(0), RIGHT(1), LINE(2);

        private final int value;

        ItemType(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }

    public static class BaseEntity {
        protected BaseAttributeDescView.ItemType type = LEFT;
        protected String content;

        public BaseAttributeDescView.ItemType getType() {
            return type;
        }

        public void setType(BaseAttributeDescView.ItemType type) {
            this.type = type;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        @Override
        public String toString() {
            return "BaseEntity{" +
                    "type=" + type +
                    ", content='" + content + '\'' +
                    '}';
        }
    }
}
